<?php
namespace app\api\controller\auth;


use app\api\controller\Api;
use app\common\system\model\AuthNode;
use app\common\system\validate\AuthNodeValidate;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\exception\ErrorException;
use util\Tree;

class Node extends Api
{
    public function tree()
    {
        $model = new AuthNode();
        $fields = 'create_time,update_time,delete_time';
        $rows = $model->withoutField($fields)->order('status desc,type,sort,id')->select();
        if (!$rows->isEmpty()) {
            foreach ($rows as $key => &$row) {
                $row->append(['meta']);
            }

            $list = $rows->toArray();
            $result = Tree::buildTree([], $list, 0);
        } else {
            $result = [];
        }

        return success($result);
    }

    // 读取节点信息
    public function read()
    {
        $param = $this->request->param();
        if (empty($param['id'])) return error([], '参数错误');

        
        try {

            $fields = "create_time,update_time,delete_time";
            $model = new AuthNode();
            $result = $model->where('id', $param['id'])->withoutField($fields)->find();

            return success($result);

        } catch (DataNotFoundException $e) {
            return error([], '已不存在');
        }
    }

    // 创建节点
    public function create()
    {
        $post = $this->request->post();
        // 创建的时候清除干扰的信息
        unset($post['id']);
        // 验证提交
        $validate = new AuthNodeValidate();
        if (!$validate->check($post)) return error([], $validate->getError());

        $model = new AuthNode();
        $user = $this->request->user;
        $post['operator_id'] = $user['id'];
        try {
            $model->save($post);
        } catch (DbException $e) {
            return error([], "添加节点失败");
        } catch (ErrorException $e) {
            return error([], "写入异常");
        }

        if ($model->id > 0) {
            return success([], '添加节点成功');
        } else {
            return error([], '添加节点失败');
        }
    }

    // 编辑节点信息
    public function save()
    {
        $id = $this->request->param('id');
        if (empty($id)) return error([], '参数错误');

        // 验证提交
        $post = $this->request->put();
        $validate = new AuthNodeValidate();
        if (!$validate->check($post)) return error([], $validate->getError());

        $model = new AuthNode();
        $row = $model->where('id', $id)->find();
        if (empty($row)) return error([], '节点不存在');

        $user = $this->request->user;
        $post['operator_id'] = $user['id'];
        $rs = $row->save($post);

        if ($rs) {
            return success([], '编辑节点成功');
        } else {
            return error([], '编辑节点失败');
        }

    }

    // 删除节点
    public function delete()
    {
        $id = $this->request->param('id');
        if (empty($id)) return error([], '参数错误');

        $model = new AuthNode();
        $children = $model->where('parent_id', $id)->find();
        if (!empty($children)) return error([], '存在子节点不能删除');

        $row = $model->where('id', $id)->find();
        if (empty($row)) return error([], '节点已不存在');
        $rs = $row->delete();

        if ($rs) {
            return success([], '删除成功');
        } else {
            return error([], '删除失败');
        }
    }
}